package com.gjy.sentinel.core.demo;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;

/**
 * 热点参数限流
 *
 * @author gjy
 * @version 1.0
 * @since 2025-10-15 15:43:19
 */
public class ParamFlowDemo {

    private static final String RESOURCE = "GetUserById";

    /**
     * @param userId 假设是热点键（如用户 ID）
     * @return true 表示通过，false 表示被热点限流
     */
    public static boolean tryAccess(int userId) {
        Entry entry = null;
        try {
            entry = SphU.entry(RESOURCE, EntryType.OUT, 1, userId);
            System.out.println("[ParamFlowDemo] userId=" + userId + " 通过热点限流");
            return true;
        } catch (BlockException be) {
            System.out.println("[ParamFlowDemo] userId=" + userId + " 被热点限流");
            return false;
        } finally {
            if (entry != null) entry.exit();
        }
    }

}
